package com.cosc716.application.gui.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JPanel;

import com.cosc716.restaurant.domain.Order;
import com.cosc716.restaurant.domain.collections.OrderQueue;
import com.cosc716.restaurant.domain.collections.WaitingQueue;
import com.cosc716.restaurant.domain.collections.WaitingQueueIterator;
import com.cosc716.restaurant.system.SystemInterface;
import com.cosc716.simulator.Simulator;


public class ManagementPanel extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 4306679282188373138L;
	
	private final String RUN_SIMULATOR_COMMAND = "RUN_SIMULATOR_COMMAND";
	private final String RUN_SIMULATOR_STEP_COMMAND = "RUN_SIMULATOR_STEP_COMMAND";
	private final String RESET_SIMULATOR_COMMAND = "RESET_SIMULATOR_COMMAND";
	
	private SystemInterface systemInterface;
	private Simulator simulator;
	
	private ListPanel orderQueueListPanel;
	private ListPanel waitQueueListPanel;
	
	public ManagementPanel(SystemInterface systemInterface, Simulator simulator) {
		this.systemInterface = systemInterface;
		this.simulator = simulator;
		
		addComponents();
	}
	
	private void addComponents() {

		this.setLayout(new GridLayout(1,3));
		
		JPanel buttonPanel = new JPanel();
		JButton runSimulatorButton = new JButton("Run Simulator");
		runSimulatorButton.setActionCommand(RUN_SIMULATOR_COMMAND);
		runSimulatorButton.addActionListener(this);
		buttonPanel.add(runSimulatorButton);
		
		JButton runSimulatorStepButton = new JButton("Run Simulator Step");
		runSimulatorStepButton.setActionCommand(RUN_SIMULATOR_STEP_COMMAND);
		runSimulatorStepButton.addActionListener(this);
		buttonPanel.add(runSimulatorStepButton);
		
		JButton resetSimulatorButton = new JButton("Reset Simulator");
		resetSimulatorButton.setActionCommand(RESET_SIMULATOR_COMMAND);
		resetSimulatorButton.addActionListener(this);
		buttonPanel.add(resetSimulatorButton);
		
		SimulatorPanel simulatorPanel = new SimulatorPanel(simulator);
		
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = 1.0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		
		gbc.gridx = gbc.gridy = 0;
		controlPanel.add(buttonPanel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		controlPanel.add(simulatorPanel, gbc);

		JPanel spacedPanel = new JPanel();
		spacedPanel.setLayout(new GridLayout(3, 1));
		
		spacedPanel.add(controlPanel);
		spacedPanel.add(new JPanel());
		spacedPanel.add(new JPanel());
		
		orderQueueListPanel = new ListPanel("Order Queue");
		waitQueueListPanel = new ListPanel("Wait Queue");

		this.add(spacedPanel);
		this.add(orderQueueListPanel);
		this.add(waitQueueListPanel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(RUN_SIMULATOR_COMMAND)) {
			simulator.simulate();
			
			updateQueueLists();
		}
		else if (e.getActionCommand().equals(RUN_SIMULATOR_STEP_COMMAND)) {
			simulator.step();
			
			updateQueueLists();
		}
		
		else if (e.getActionCommand().equals(RESET_SIMULATOR_COMMAND)) {
			
			// remove all from all queues
			systemInterface.clearWaitingQueue();
			systemInterface.clearAllTables();
			systemInterface.clearOrderQueue();
			
			updateQueueLists();
		}
		
	}
	
	private void updateQueueLists() {
		updateOrderQueueList();
		updateWaitQueueList();
	}
	
	private void updateOrderQueueList() {
		OrderQueue orders = systemInterface.getOrders();
		
		orderQueueListPanel.clearList();
		
		Iterator<Order> queueIterator = orders.getOrderIterator();
		while(queueIterator.hasNext()) {
			orderQueueListPanel.addListItem(queueIterator.next().toString());
		}
		
		System.out.println();
	}
	
	private void updateWaitQueueList() {
		WaitingQueue customerQueue = systemInterface.getWaitingQueue();
		
		waitQueueListPanel.clearList();
		
		WaitingQueueIterator queueIterator = customerQueue.getQueueIterator();
		//Iterator<Customer> queueIterator = customerQueue.getQueueIterator();
		while(queueIterator.hasNext()) {
			waitQueueListPanel.addListItem(queueIterator.next().toString());
		}
	}
}